const api = require("../../server/Api.js");
const util = require("../../utils/util.js");
const app = getApp();

//订单详情 状态 99-全部 0-已取消 1-待付款 2-待发货 3-待收货 4-已关闭 5-已完成 88-待评价
Page({
  /**
   * 页面的初始数据
   */
  data: {
    curPage: 1,
    pageSize: 6,
    hasMore: true,
    items: [], //总商品数

    showPayTypeDialog: false,
    showCancelDialog: false, //取消订单弹窗
    curPayType: 0,

    operType: 1, //1:取消订单  2：删除订单
    curOrder: {}, //按钮操作的当前id
    curOrderIndex: 0, //当前订单的索引
    emptyType: 2,
    curTap: 99,
    scrollLeft: 0,
    // 订单类型
    orderType: 0,
    tabTitles: [{
        name: "全部",
        type: 1
      }, {
        name: "待发货",
        type: 2
      },
      {
        name: "待收货",
        type: 3
      }, {
        name: "已收货",
        type: 4
      }
    ],
    opertionBtns: {
      0: [{
        name: '再来一单',
        color: 'red'
      }],
      1: [{
        name: '取消订单',
        color: 'gray'
      }, {
        name: '立即付款',
        color: 'red'
      }],
      2: [{
        name: '取消订单',
        color: 'gray'
      }, {
        name: '再来一单',
        color: 'red'
      }],
      3: [{
        name: '取消订单',
        color: 'gray'
      }, {
        name: '再来一单',
        color: 'red'
      }],
      4: [{
        name: '再来一单',
        color: 'red'
      }],
      5: [{
        name: '再来一单',
        color: 'red'
      }],
      //待自提
      98: [{
        name: '取消订单',
        color: 'gray'
      }, {
        name: '再来一单',
        color: 'red'
      }],
      //已自提
      97: [{
        name: '再来一单',
        color: 'red'
      }],
      //已退款
      96: [{
        name: '再来一单',
        color: 'red'
      }]
    },
  },

  onLoad: function(options) {
    if (options.orderType) {
      let curTabIndex = parseInt(options.orderType);
      this.setData({
        curTap: curTabIndex,
        scrollLeft: (parseInt(options.orderType) - 1) * 60
      })
    }
    //this.refreshData();
  },

  onShow: function() {
    let userInfo = app.globalData.userBean;
    if (!(userInfo && userInfo.consumerId)) {
      tt.switchTab({
        url: '../main-person-new/main-person',
        success() {
          tt.navigateTo({
            url: '../login/login',
          })
        }
      })
      return
    }
    this.refreshData();
  },
  //下拉啊刷新
  onPullDownRefresh: function() {
    this.refreshData();
  },

  //刷新列表
  refreshData: function() {
    let userInfo = app.globalData.userBean;
    if (!(userInfo && userInfo.consumerId)) {
      tt.switchTab({
        url: '../main-shop-list/main-shop-list',
        success() {
          tt.navigateTo({
            url: '../login/login',
          })
        }
      })
      return
    }
    // tt.showNavigationBarLoading();
    //app.showLoading();
    var that = this;
    this.setData({
      hasMore: true,
    })
    this.queryOrderList(1, this.data.pageSize);
  },

  //上拉加载
  onReachBottom: function() {
    if (this.data.hasMore == false) return;
    // tt.showNavigationBarLoading();
    let page = this.data.curPage + 1;
    this.queryOrderList(page, this.data.pageSize);
  },

  //分页查询订单
  queryOrderList: function(page, pageSize) {
    let that = this;
    let userBean = app.globalData.userBean;
    let orderStatus = this.data.curTap;
    let prams = {
      pageSize: that.data.pageSize,
      pageNumber: page,
      userId: userBean.consumerId,
      status: orderStatus
    };
    api.queryOrderList(prams).then(res => {
      //请求成功
      that.querySuccess(res, page)
      tt.stopPullDownRefresh();
      that.setData({
        emptyType: 2
      })
    }).catch(msg => {
      tt.stopPullDownRefresh();
      that.setData({
        emptyType: 101
      })
      var message = typeof msg == "string" ? msg : msg.message;
      app.showToast(message);
    })
  },

  //查询成功
  querySuccess: function(res, curPage) {
    let that = this;
    //数据
    for (let i = 0; i < res.records.length; i++) {
      res.records[i] = util.formatOrderStatus(res.records[i])
      res.records[i].orderCnStatus = "订单" + res.records[i].orderCnStatus;
      if (res.records[i].orderStatus === 98 && res.records[i].payStatus === 1 && res.records[i].payTime && res.records[i].list && res.records[i].list.length > 0 && res.records[i].list[0].receiptInterval) {
        res.records[i].orderCnStatus = "预计" + app.getZtDate(res.records[i].deliveryTime) + "可提货"
      }
      res.records[i].store = {
        storeName: res.records[i].storeName,
        address: res.records[i].address
      }
    }
    let records = res.records;
    let maxPage = parseInt(res.pages);
    var datas = curPage == 1 ? [] : this.data.items;
    if (records) {
      datas = datas.concat(records);
    }
    that.setData({
      hasMore: maxPage > curPage,
      items: datas,
      curPage: curPage,
    })
  },

  // 按钮操作
  opertionTap: function(e) {
    let order = e.currentTarget.dataset.order;
    let shopIndex = e.currentTarget.dataset.shopindex;
    let btnName = e.currentTarget.id;
    let opertionBtns = this.data.opertionBtns;
    this.setData({
      curOrder: order,
      curOrderIndex: shopIndex
    })


    switch (btnName) {
      case "取消订单":
        this.setData({
          operType: 1
        })
        this.showCancelDialog();
        break;
      case "删除订单":
        this.setData({
          operType: 2
        })
        this.showCancelDialog();
        break;
      case "立即付款":
        this.showPayTypeDialog();
        break;
      case "再来一单":
        tt.switchTab({
          url: '../index/index'
        })
        break
    }
  },

  //请求删除or取消订单
  onConfirm: function() {
    let that = this;
    let curOrder = this.data.curOrder;
    let curOrderIndex = this.data.curOrderIndex;

    let operType = this.data.operType;
    if (operType == 2) {
      app.showLoading()
      api.deleteOrder(curOrder.orderId).then(res => {
        tt.hideLoading()
        var shops = that.data.items;
        shops.splice(curOrderIndex, 1);
        that.setData({
          items: shops
        })
      }).catch(msg => {
        tt.hideLoading()
        let message = typeof msg == "string" ? msg : msg.message;
        app.showToast(message);
      })
    } else {
      api.cancelOrder(curOrder.orderId).then(res => {
        let itemIndex = "items[" + curOrderIndex + "]"
        curOrder.orderStatus = 0;
        that.setData({
          [itemIndex]: curOrder
        })
      }).catch(msg => {
        let message = typeof msg == "string" ? msg : msg.message;
        app.showToast(message);
      })
    }
  },

  //显示是否弹出取消订单的操作
  showCancelDialog: function() {
    this.setData({
      showCancelDialog: true,
    })
  },

  //头部导航栏选择事件
  tabSelect: function(e) {
    let tabBean = e.currentTarget.dataset.tab;
    this.setData({
      curTap: tabBean.type,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
    this.refreshData();
  },

  //显示支付方式弹窗
  showPayTypeDialog: function() {
    this.setData({
      showPayTypeDialog: true,
    })
  },
  //隐藏所有弹窗
  hiddenDialog: function(e) {
    this.setData({
      showPayTypeDialog: false,
      showCancelDialog: false
    })
  }

})